import Vue from 'vue'
import Vuex from 'vuex'
//
import {saveLocal,getLocal,clearLocal} from '@/utils/util'
//
Vue.use(Vuex)
//
export default new Vuex.Store({
  modules:{},
  state: {
    token:'',
    menus:undefined,
    buttons:undefined,
    userInfo:undefined
  },
  getters:{
    token:state => state.token || getLocal('token'),
    menus:state => state.menus || getLocal('menus','object'),
    buttons:state => state.buttons || getLocal('buttons','object'),
    userInfo:state => state.buttons || getLocal('userInfo','object'),
  },
  mutations: {
    setToken(state,val){state.token = val;saveLocal('token',val)},
    setMenus(state,val){state.menus = val;saveLocal('menus',val)},
    setButtons(state,val){state.buttons = val;saveLocal('buttons',val)},
    setUserInfo(state,val){state.userInfo = val;saveLocal('userInfo',val)},
  },
  actions: {
    // 登出时可以调用，清空缓存的用户数据
    resetUserData({state},isClear = true){
      // 清除登陆后保存的数据
      state.token = '';
      state.menus = undefined;
      state.buttons = undefined;
      state.userInfo = undefined;
      //
      isClear && clearLocal()
    }
  },
})
